#
# Copyright (c) 2022, Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#  * Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#  * Neither the name of Intel Corporation nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

cmake_minimum_required(VERSION 3.18)
#Consider building from sources by including cmake from QVL
#set(QVL_SRC ${CMAKE_CURRENT_LIST_DIR}/../../../../QVL/QVL/Src/CMakeLists.txt)
#include(${QVL_SRC})

project (QuoteVerificationLibraryWrapper)

if(NOT DEFINED QVL_PATH)
    message(FATAL_ERROR "You must provide QVL_PATH")
endif()
message(STATUS "QVL_PATH set to ${QVL_PATH}")

set(CMAKE_DEBUG_POSTFIX d)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(QVL_LIB_NAME "QuoteVerificationStaticd")
    set(ATTESTATIONPARSERS_LIB_NAME "AttestationParsersStaticd")
    set(ATTESTATIONCOMMONS_LIB_NAME "AttestationCommonsStaticd")
else()
    set(QVL_LIB_NAME "QuoteVerificationStatic")
    set(ATTESTATIONPARSERS_LIB_NAME "AttestationParsersStatic")
    set(ATTESTATIONCOMMONS_LIB_NAME "AttestationCommonsStatic")
endif()
message(STATUS "QVL_LIB_NAME set to ${QVL_LIB_NAME}")
message(STATUS "ATTESTATIONPARSERS_LIB_NAME set to ${ATTESTATIONPARSERS_LIB_NAME}")


find_library(QVL NAMES ${QVL_LIB_NAME} PATHS ${QVL_PATH}/lib)
find_library(AttestationParsers NAMES ${ATTESTATIONPARSERS_LIB_NAME} PATHS ${QVL_PATH}/lib)
find_library(AttestationCommons NAMES ${ATTESTATIONCOMMONS_LIB_NAME} PATHS ${QVL_PATH}/lib)

file(GLOB SOURCE_FILES *.cpp *.h)
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})

set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 17)
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")

execute_process(COMMAND node -p "require('node-addon-api').include"
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        OUTPUT_VARIABLE NODE_ADDON_API_DIR
        )
string(REPLACE "\n" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
string(REPLACE "\"" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})

if(NOT DEFINED CMAKE_JS_INC)
    message(FATAL_ERROR "You must provide CMAKE_JS_INC")
endif()
message(STATUS "CMAKE_JS_INC set to ${CMAKE_JS_INC}")

target_include_directories(${PROJECT_NAME}
    PRIVATE ${NODE_ADDON_API_DIR}
    PRIVATE ${CMAKE_JS_INC}
    PRIVATE ${QVL_PATH}/include)

target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB} ${QVL} ${AttestationParsers} ${AttestationCommons})
